window.onload = function () {
    const {createEditor, createToolbar} = window.wangEditor;

    const editorConfig = {
        placeholder: '请输入内容...',
        onChange(editor) {
            const html = editor.getHtml();
            console.log('editor content', html);
            // 也可以同步到 <textarea>
        }
    };

    const editor = createEditor({
        selector: '#editor-container',
        html: '<p><br></p>',
        config: editorConfig,
        mode: 'default', // or 'simple'
    });

    const toolbarConfig = {};

    const toolbar = createToolbar({
        editor,
        selector: '#toolbar-container',
        config: toolbarConfig,
        mode: 'default', // or 'simple'
    });

    $('#submit-btn').click(function (event) { // 修改这一行，移除 TypeScript 语法
        // 阻止按钮的默认行为
        event.preventDefault();
        const content = editor.getHtml(); // 获取编辑器内容
        // 获取编辑器内容
        let title = $("input[name='title']").val();
        let category = $("#category-select").val();
        let csrfmiddlewaretoken = $('input[name="csrfmiddlewaretoken"]').val();
        $.ajax('/blog/pub', // 替换为你的后端发布接口
            {
                method: 'POST',
                data: {
                    title: title,
                    category: category,
                    content: content,
                    csrfmiddlewaretoken: csrfmiddlewaretoken,
                },
                success: function (result) {
                    if(result['code'] === 200){
                        // 获取博客id
                        let blog_id = result['data']['blog_id'];
                        // 跳转到博客详情
                       window.location = '/blog/' + blog_id;
                    }else{
                        alert(result['message']);
                    }
                }
            }
        );
    });
};
